package com.zhangzhan.gulimallproduct.app;

import cn.hutool.core.collection.CollUtil;
import com.zhangzhan.gulimallcommcon.util.PageUtils;
import com.zhangzhan.gulimallcommcon.util.R;
import com.zhangzhan.gulimallproduct.entity.CategoryBrandRelationEntity;
import com.zhangzhan.gulimallproduct.service.CategoryBrandRelationService;
import com.zhangzhan.gulimallproduct.vos.BrandVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 品牌分类关联
 *
 * @author zhangzhan
 * @email sunlightcs@gmail.com
 * @date 2024-06-12 16:45:44
 */
@RestController
@RequestMapping("gulimallproduct/categorybrandrelation")
public class CategoryBrandRelationController {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("gulimallproduct:categorybrandrelation:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = categoryBrandRelationService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping(value = "/catelog/list", method = RequestMethod.GET)
    //@RequiresPermissions("gulimallproduct:categorybrandrelation:list")
    public R catelogList(@RequestParam Map<String, Object> params) {
        return R.ok().put("data", categoryBrandRelationService.catelogList(params));
    }


    /**
     * 获取品牌关联的分类
     */
    @RequestMapping(value = "/brands/list", method = RequestMethod.GET)
    //@RequiresPermissions("gulimallproduct:categorybrandrelation:list")
    public R brandList(@RequestParam(value = "catId", required = true) Long catId) {
        List<CategoryBrandRelationEntity> categoryBrandRelation = categoryBrandRelationService.brandListByCatId(catId);

        List<BrandVo> vos = null;
        if (CollUtil.isNotEmpty(categoryBrandRelation)) {
            vos = categoryBrandRelation.stream().map(po -> {
                BrandVo brandVo = new BrandVo();
                brandVo.setBrandId(po.getBrandId());
                brandVo.setBrandName(po.getBrandName());
                return brandVo;
            }).toList();
        }

        return R.ok().put("data", vos);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("gulimallproduct:categorybrandrelation:info")
    public R info(@PathVariable("id") Long id) {
        CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);

        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("gulimallproduct:categorybrandrelation:save")
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation) {
        categoryBrandRelationService.saveDetail(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("gulimallproduct:categorybrandrelation:update")
    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation) {
        categoryBrandRelationService.updateById(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("gulimallproduct:categorybrandrelation:delete")
    public R delete(@RequestBody Long[] ids) {
        categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
